package cn.tiansu.eway.kylinengine.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

import org.jfree.util.Log;

/**
 * 时间函数工具类
 * 
 * @author user
 *
 */
public class DataTimeUtils
{
	public static String timeFotmat1 = "yyyy-MM-dd HH:mm:ss";

	public static String timeFotmat2 = "yyyyMMddHHmmss";
	
	public static String timeFotmat3 = "yyyy-MM-dd";

	private Calendar calendar = Calendar.getInstance(TimeZone
			.getTimeZone("GMT+8:00"));

	/**
	 * 本地时间 转化为UTC时间，默认时区为东八区
	 * 
	 * @param dateStr
	 *            时间字符串
	 * @param timeFormat
	 *            格式字符串
	 * @return 时间毫秒数
	 */
	public long localTime2UTCTime(String dateStr, String timeFormat)
	{
		SimpleDateFormat df = new SimpleDateFormat(timeFormat);
		df.setTimeZone(TimeZone.getTimeZone("UTC"));
		try
		{
			return df.parse(dateStr).getTime();
		} catch (ParseException e)
		{
			e.printStackTrace();
		}

		return 0;
	}

	/**
	 * utc时间转为为本地时间，默认时区为东八区
	 * 
	 * @param utcTime
	 *            utc时间
	 * @param utcTimeFormat
	 *            utc时间格式
	 * @param localTimeFormat
	 *            本地时间格式
	 * @return 本地时间字符串
	 */
	public String utcTime2LocalTime(String utcTime,
			String utcTimeFormat, String localTimeFormat)
	{
		SimpleDateFormat utcFormater = new SimpleDateFormat(utcTimeFormat);
		utcFormater.setTimeZone(TimeZone.getTimeZone("UTC"));// 时区定义并进行时间获取
		Date gpsUTCDate = null;
		try
		{
			gpsUTCDate = utcFormater.parse(utcTime);
		} catch (ParseException e)
		{
			e.printStackTrace();
		}
		SimpleDateFormat localFormater = new SimpleDateFormat(localTimeFormat);
		localFormater.setTimeZone(TimeZone.getDefault());
		String localTime = localFormater.format(gpsUTCDate.getTime());
		return localTime;
	}

	/**
	 * 取日期 yyyymmddhh
	 * 
	 * @return String
	 */
	public String getHourDate()
	{
		String months, days, years, hours;
		years = String.valueOf(getYear());
		if (getMonth() < 10)
		{
			months = "0" + getMonth();
		} else
		{
			months = String.valueOf(getMonth());
		}
		if (getDay() < 10)
		{
			days = "0" + getDay();
		} else
		{
			days = String.valueOf(getDay());
		}
		if (getHour() < 10)
		{
			hours = "0" + getHour();
		} else
		{
			hours = String.valueOf(getHour());
		}
		return years + "" + months + "" + days + "" + hours;
	}

	/**
	 * 取日期 yyyy-mm-dd
	 * 
	 * @return String
	 */
	public String getDayDate()
	{
		String months, days, years;
		years = String.valueOf(getYear());
		if (getMonth() < 10)
		{
			months = "0" + getMonth();
		} else
		{
			months = String.valueOf(getMonth());
		}
		if (getDay() < 10)
		{
			days = "0" + getDay();
		} else
		{
			days = String.valueOf(getDay());
		}
		return years + "-" + months + "-" + days;
	}

	/**
	 * 取年 yyyy
	 * 
	 * @return int
	 */
	public int getYear()
	{
		return calendar.get(Calendar.YEAR);
	}

	/**
	 * 取月 mm
	 * 
	 * @return int
	 */
	public int getMonth()
	{
		return calendar.get(Calendar.MONTH) + 1;
	}

	/**
	 * 取日 dd
	 * 
	 * @return int
	 */
	public int getDay()
	{
		return calendar.get(Calendar.DAY_OF_MONTH);
	}

	/**
	 * 构造函数：初始化时间发生器（带参数:DateTime） 参数格式：yyyy-MM-dd HH:mm:ss or yyyy-MM-dd
	 */
	public Date getDateTime(String DateTime)
	{
		Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("GMT+8:00"));
		SimpleDateFormat s = null;
		Date date = null;
		try
		{
			if (DateTime.length() > 12)
			{
				s = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			} else
			{
				s = new SimpleDateFormat("yyyy-MM-dd");
			}
			date = new Date();
			try
			{
				date = s.parse(DateTime);
			} catch (ParseException e)
			{
				System.out.println("e:" + e.getMessage());
			}
		} catch (Exception ee)
		{
			System.out.println("ee:" + ee.getMessage());
		}
		cal.setTime(date);

		return cal.getTime();
	}

	/**
	 * 构造函数：初始化时间发生器（带参数: 长整型时间数据：秒）
	 */
	public Date getDateTime(long serverDateTime)
	{
		Calendar calendar = Calendar.getInstance(TimeZone
				.getTimeZone("GMT+8:00"));
		calendar.setTimeInMillis(serverDateTime); // 指定时间
		return calendar.getTime();
	}

	/**
	 * 构造函数：初始化时间发生器（带参数:DateTime） 参数格式：yyyy-MM-dd HH:mm:ss or yyyy-MM-dd
	 */
	public long getLongDateTime(String DateTime)
	{
		Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("GMT+8:00"));
		SimpleDateFormat s = null;
		Date date = null;
		try
		{
			if (DateTime.length() > 12)
			{
				s = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			} else
			{
				s = new SimpleDateFormat("yyyy-MM-dd");
			}
			date = new Date();
			try
			{
				date = s.parse(DateTime);
			} catch (ParseException e)
			{
				System.out.println("e:" + e.getMessage());
			}
		} catch (Exception ee)
		{
			System.out.println("ee:" + ee.getMessage());
		}
		cal.setTime(date);

		return cal.getTimeInMillis();
	}

	/**
	 * 获取偏移时间，正数为往后偏移，负数为往前偏移
	 * 
	 * @param year
	 *            年偏移,0为不偏移
	 * @param month
	 *            月偏移,0为不偏移
	 * @param day
	 *            日偏移,0为不偏移
	 * @return date时间
	 */
	public Date getNextDate(int year, int month, int day)
	{
		Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("GMT+8:00"));

		cal.add(Calendar.YEAR, year);
		cal.add(Calendar.MONTH, month);
		cal.add(Calendar.DATE, day);

		return cal.getTime();
	}

	/**
	 * 获取偏移时间，正数为往后偏移，负数为往前偏移
	 * 
	 * @param year
	 *            年偏移,0为不偏移
	 * @param month
	 *            月偏移,0为不偏移
	 * @param day
	 *            日偏移,0为不偏移
	 * @return date时间
	 */
	public String getNextDate(String date, int year, int month, int day)
	{
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		try
		{
			calendar.setTime(df.parse(date));
			calendar.add(Calendar.YEAR, year);
			calendar.add(Calendar.MONTH, month);
			calendar.add(Calendar.DATE, day);
		} catch (ParseException e)
		{
			Log.error("", e);
		}

		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

		return sdf.format(calendar.getTime());
	}

	public static void main(String[] args)
	{
		// System.out.println(getNextDate(0, 0, 2));
		// System.out.println(utcTime2LocalTime("2015-08-10 12:40:20",
		// "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm:ss"));

		// System.out.println(localTime2UTCTime("2016-02-24 00:00:00",
		// "yyyy-MM-dd HH:mm:ss"));
		// 1456272000000

		DataTimeUtils dt = new DataTimeUtils();
		System.out.println(dt.getNextDate("2016-02-25", 0, 0, 1));
	}

	/**
	 * 取小时 hh
	 * 
	 * @return int
	 */
	public int getHour()
	{
		return calendar.get(Calendar.HOUR_OF_DAY);
	}

}
